package com.yh.workbill.ui.leaseInfo;


import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.model.Company;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.ui.companyInfo.CompanyInfoRepository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class LeaseInfoViewModel extends ViewModel {
    private LeaseInfoRepository repository;

    public LeaseInfoViewModel() {
        this.repository = new LeaseInfoRepository();
    }

    public LiveData<List<UnitCompany>> getAllUnitCompanys() {
        return repository.data;
    }

    public void del(UnitCompany unitCompany) {
        this.repository.del(unitCompany);
    }

    public void add(UnitCompany unitCompany) {
        this.repository.add(unitCompany);
    }

    public MutableLiveData<List<UseCompany>> useCompanys;


    public MutableLiveData<List<UseCompany>> getUseCompanys() {
        if (useCompanys == null) {
            useCompanys = new MutableLiveData<>();
        }
        return useCompanys;
    }


    public void addUseCompanydatas(int index, UseCompany... useCompanies) {
        List<UseCompany> value = useCompanys.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        List<UseCompany> temp = Arrays.asList(useCompanies);
        if (index == 0) {//新增
            value.addAll(0, temp);
            useCompanys.postValue(value);
        } else {//更多
            value.addAll(temp);
            useCompanys.postValue(value);
        }
    }

    public void update() {
        List<UseCompany> value = useCompanys.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        useCompanys.postValue(value);
    }
}
